package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {


        @Autowired
        private RedisTemplate redisTemplate;

        @PostMapping("send")
        public Result ValidateCode (String telephone){

                //生成验证码
                String validateCode = ValidateCodeUtils.generateValidateCode(6).toString();
                //控制台打印模拟发送验证码
                System.out.println(validateCode);
                log.info("[发送手机验证码]手机号:{} 验证码:{}",telephone,validateCode);

                //存验证码到redis
                redisTemplate.opsForValue().set(telephone,validateCode,30,TimeUnit.MINUTES);


//                //取值测试
//                String getdata =(String) redisTemplate.opsForValue().get(telephone);
//                System.out.println(getdata);

                return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
        }

}
